package com.kaiyiweitong.tongxing.activity;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.fragment.ClassifyFragment;
import com.kaiyiweitong.tongxing.fragment.CouponFragment;
import com.kaiyiweitong.tongxing.fragment.HomeFragment;
import com.kaiyiweitong.tongxing.fragment.MyFragment;
import com.kaiyiweitong.tongxing.fragment.SearchFragment;
import com.kaiyiweitong.tongxing.other.FragmentFactory;
import com.kaiyiweitong.tongxing.util.CommonUtil;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private static final String TAG = "MainActivity";
    private int currentFragmentIndex = -1;
    private RadioGroup mMainRg;

    Class[] fragmentClass = new Class[]{
            HomeFragment.class,
            ClassifyFragment.class,
            SearchFragment.class,
            CouponFragment.class,
            MyFragment.class
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_main);
        requestPermission();
        init();
    }

    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
//                    Manifest.permission.ACCESS_FINE_LOCATION,
//                    Manifest.permission.CALL_PHONE,
//                    Manifest.permission.READ_LOGS,
//                    Manifest.permission.READ_PHONE_STATE,
//                    Manifest.permission.SET_DEBUG_APP,
//                    Manifest.permission.SYSTEM_ALERT_WINDOW,
//                    Manifest.permission.GET_ACCOUNTS,
//                    Manifest.permission.WRITE_APN_SETTINGS
            };
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }
    }

    private void init() {
        mMainRg = (RadioGroup) findViewById(R.id.main_rg);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < fragmentClass.length; i++) {
            transaction.add(R.id.container, FragmentFactory.getInstance(fragmentClass[i]));
            transaction.hide(FragmentFactory.getInstance(fragmentClass[i]));
        }
        transaction.commit();
        mMainRg.setOnCheckedChangeListener(this);
        ((RadioButton) mMainRg.getChildAt(0)).setChecked(true);//默认展示首页
        showFragment(0);
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_home:
                showFragment(0);
                break;
            case R.id.rb_classify:
                showFragment(1);
                break;
            case R.id.rb_search:
                showFragment(2);
                break;
            case R.id.rb_coupon:
                showFragment(3);
                break;
            case R.id.rb_my:
                showFragment(4);
                break;
        }
    }

    private synchronized void showFragment(int rbIndex) {
        if (rbIndex != currentFragmentIndex) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (currentFragmentIndex != -1) {
                transaction.hide(FragmentFactory.getInstance(fragmentClass[currentFragmentIndex]));
            }
            transaction.show(FragmentFactory.getInstance(fragmentClass[rbIndex]));
            transaction.commit();
            currentFragmentIndex = rbIndex;
        }
    }


    private static boolean isExit = false;      //是否退出

    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            isExit = false;
            return true;
        }
    });

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();                      //自己处理按键事件
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 1秒间隔内连续点击回退键2次退出应用
     */
    private void exit() {
        if (isExit) {
            finish();
            System.exit(0);
        } else {
            isExit = true;
            CommonUtil.toast(this, R.string.to_exit_app);
            mHandler.sendEmptyMessageDelayed(0, 1000);          // 利用handler延迟发送更改状态信息
        }
    }
}